%% Data Synthesis Script
% This script extracts the data extraction and critical appraisal data from
% the output from Cadima and synthesises the data. It furthermore calculates 
% some summary statistics  that are used in:
% - The QUADAS-2 Excel bar chart sheet 
% - Figure 3 and 4 (Correlation Figure).

% Script Sub-components
% 1) Select folders and files
% 2) Download CA-excel sheet and obtain only the required columns and rows
% 3) Add the 'NIRS-parameter' variable from DA_table to CA_table_new
% 4) Change header names to s1,s2, etc...
% 5) Per domain, classify the studies based on the logical flowchart
% 6) For the consensus-meeting studies, hardcode the classification (to make sure that classification is correct)
% 7) Separation on real substudies and then CA classification
% 8) Removal of studies with <50% Low RoB/Applicability classifications
% 9) Calculate statistics for the QUADAS excel bar chart sheet
% 10) Calculate the 'Sum quality score' for each study - For Circle size correlation figure (Figure 3&4)

% drs. Nick Eleveld, University Medical Centre Groningen, 21 December 2022
%% 1) Select folders and files
data_folder   = 'PRIVATE';
input_file_CA = 'critical_appraisal_outcome_2022_10_27.xlsx';
input_file_DA = 'data_extraction_sheet_2022_10_27.xlsx';
output_file_name = 'Critical_Appraisal_domains_2022_10_27.csv';

%% 2) Download CA-excel sheet and obtain only the required columns and rows
CA_table = readtable(strcat(data_folder,input_file_CA),'ReadVariableNames',true);
DA_table = readtable(strcat(data_folder,input_file_DA),'ReadVariableNames',true);
% 2-sub) Add the correct headers to the columns from 'Reviewer 2'
CA_header_names = CA_table.Properties.VariableNames';
columns_new_headers = 10:4:118;
for i=1:length(columns_new_headers)
    CA_header_names(columns_new_headers(i))   = CA_header_names(columns_new_headers(i)-2);
    CA_header_names(columns_new_headers(i)-2) = {strcat('old_',num2str(columns_new_headers(i)-2))};
end
CA_table.Properties.VariableNames = CA_header_names';
% 2-sub) Remove columns and rows not containing relevant data
rows_relevant = 2:height(CA_table); % First row can be removed
columns_relevant = [1,2,3,10,14,18,22,26,30,38,42,46,50,66,70,74,78,82,86,90,94,98,102,106,118]; % Rows from 'Reviewer 2'
CA_table_new = CA_table(rows_relevant,columns_relevant);
% 2-sub) Sort rows based on column 1 (ArticleID) and 2 (StudyID)
CA_table_new=sortrows(CA_table_new,[1,2]);

%% 3) Add the 'NIRS-parameter' variable from DA_table to CA_table_new
% Select columns and add to CA_table
DA_table_selected = DA_table(:,[2,3,10]);
DA_table_selected = sortrows(DA_table_selected,[1,2]);
CA_table_new = [CA_table_new DA_table_selected];
CA_table_new = movevars(CA_table_new,{'articleId','studyId','NIRS_parameter'},'Before','WasAConsecutiveOrRandomSampleOfSubjectsEnrolled_');

articleID_same = height(CA_table_new)==sum(CA_table_new.articleId == CA_table_new.ArticleID);
studyID_same   = height(CA_table_new)==sum(CA_table_new.studyId == CA_table_new.StudyID);
rows_equals = articleID_same & studyID_same;

% 3-sub) Throw an error if the articleid and studyid from CA_table_new and DA_table are not exatly equal
if ~rows_equals  
    error('No equal rows')
else
    disp('Rows are equal')
end
% 3-sub) Remove articleId and studyId columns (from DA_table)
CA_table_new = removevars(CA_table_new,{'articleId','studyId'});
%% 4) Change header names to s1,s2, etc...
new_headers = {'s1';'s2';'s3';'i1';'i2';'i3';'ia1';'ia2';'p1';'p2';'ri1';'ri2';'ri3';'re1';'re2';'re3';'f1';'f2';'f3';'f4';'f5';'sa1'}';
header_comparison = [new_headers;CA_table_new(:,5:end).Properties.VariableNames]';
% %%%%% Check if headers names are correctly converted to s1, s2 etc..!! %%%%%
CA_table_new_backup = CA_table_new;
CA_table_new.Properties.VariableNames = [CA_table_new(:,1:4).Properties.VariableNames,new_headers];

%% 5) Per domain, classify the studies based on the logical flowchart
variable_names = {'s','i','p','ri','re','f','sa','ia','ria','rea'};
domain=array2table(strings([height(CA_table_new),length(variable_names)]),'VariableNames',variable_names);
%
for i=1:height(CA_table_new) % s: Subject selection - bias
    s1 = CA_table_new{i,{'s1'}}; s2 = CA_table_new{i,{'s2'}}; s3 = CA_table_new{i,{'s3'}};
    if strcmp(s2,'No') || strcmp(s3,'No')
        domain.s(i) = 'High';
    elseif strcmp(s2,'Unclear') || strcmp(s3,'Unclear')
        domain.s(i) = 'Unclear';
    elseif strcmp(s2,'Not applicable') || strcmp(s3,'Not applicable')
        domain.s(i) = 'Not applicable'; % Possible missed entry
    elseif strcmp(s2, 'Yes') && strcmp(s3,'Yes')
        domain.s(i) = 'Low';
    end
end         
for i=1:height(CA_table_new) % sa: Subject selection - Applicability
    sa1 = CA_table_new{i,{'sa1'}};
    if strcmp(sa1,'Not applicable')
        domain.sa(i) = 'Not applicable'; % Possible missed entry
    elseif strcmp(sa1,'No')
        domain.sa(i) = 'High';
    elseif strcmp(sa1,'Unclear')
        domain.sa(i) = 'Unclear';
    elseif strcmp(sa1,'Yes')
        domain.sa(i) = 'Low'; % Possible missed entry
    end
end
for i=1:height(CA_table_new) % i: Index test - bias
    i1 = CA_table_new{i,{'i1'}}; i2 = CA_table_new{i,{'i2'}}; i3 = CA_table_new{i,{'i3'}};
    if strcmp(i1,'No') || strcmp(i2,'No')
        domain.i(i) = 'High';
    elseif strcmp(i1,'Unclear') || strcmp(i2,'Unclear')
        domain.i(i) = 'Unclear';
    elseif strcmp(i1,'Not applicable') % i2 (thresholds) can be N/A (4)
        domain.i(i) = 'ERROR'; % Possible missed entry
    elseif strcmp(i1,'Yes')
        domain.i(i) = 'Low';
    else
    end
end
for i=1:height(CA_table_new) % ia: Index test - Applicability
    ia1 = CA_table_new{i,{'ia1'}}; ia2 = CA_table_new{i,{'ia2'}};
    if strcmp(ia1,'No') || strcmp(ia2,'No')
        domain.ia(i) = 'High';
    elseif strcmp(ia1,'Unclear') || strcmp(ia2,'Unclear')
        domain.ia(i) = 'Unclear';
    elseif strcmp(ia1,'Not applicable') || strcmp(ia2,'Not applicable')
        domain.ia(i) = 'ERROR'; % Possible missed entry
    elseif strcmp(ia1,'Yes') && strcmp(ia2,'Yes')
        domain.ia(i) = 'Low';
    end
end    
for i=1:height(CA_table_new) % p: Perfusion modification
    p1 = CA_table_new{i,{'p1'}}; p2 = CA_table_new{i,{'p2'}};
    if strcmp(p1,'No')
        domain.p(i) = 'High';
    elseif strcmp(p1,'Unclear')
        domain.p(i) = 'CONSENSUS'; % Assess individually
    elseif strcmp(p1,'Yes') && strcmp(p2,'No')
        domain.p(i) = 'CONSENSUS'; % Assess individually
    elseif strcmp(p1,'Yes') && strcmp(p2,'Unclear')
        domain.p(i) = 'CONSENSUS'; % Assess individually        
    elseif strcmp(p1,'Not applicable') && strcmp(p2,'Not applicable')
        domain.p(i) = 'Not applicable';
    elseif strcmp(p1,'Yes') && strcmp(p2,'Yes')
        domain.p(i) = 'Low';
    end
end
for i=1:height(CA_table_new) % ri: Reference standard - Intracerebral 
    ri3 = CA_table_new{i,{'ri3'}};
    if strcmp(ri3,'No')
        domain.ri(i) = 'CONSENSUS';
    elseif strcmp(ri3,'Unclear')
        domain.ri(i) = 'CONSENSUS';
    elseif strcmp(ri3,'Not applicable')
        domain.ri(i) = 'Not applicable';
    elseif strcmp(ri3,'Yes')
        domain.ri(i) = 'Low';        
    end
end

for i=1:height(CA_table_new) % re: Reference standard - Extracerebral 
    re3 = CA_table_new{i,{'re3'}};
    if strcmp(re3,'No')
        domain.re(i) = 'CONSENSUS';
    elseif strcmp(ri3,'Unclear')
        domain.re(i) = 'CONSENSUS';
    elseif strcmp(re3,'Not applicable')
        domain.re(i) = 'Not applicable';
    elseif strcmp(re3,'Yes')
        domain.re(i) = 'Low';        
    end
end
%%
for i=1:height(CA_table_new) % ria: Reference standard - Intracerebral - Applicability 
    ri1 = CA_table_new{i,{'ri1'}};
    if strcmp(ri1,'No')
        domain.ria(i) = 'High';
    elseif strcmp(ri1,'Unclear')
        domain.ria(i) = 'CONSENSUS';
    elseif strcmp(ri1,'Yes')
        domain.ria(i) = 'Low'; 
    elseif strcmp(ri1,'Not applicable')
        domain.ria(i) = 'Not applicable';
    end
end

for i=1:height(CA_table_new) % rea: Reference standard - Extracerebral - Applicability 
    re1 = CA_table_new{i,{'re1'}};
    if strcmp(re1,'No')
        domain.rea(i) = 'High';
    elseif strcmp(re1,'Unclear')
        domain.rea(i) = 'CONSENSUS';
    elseif strcmp(re1,'Yes')
        domain.rea(i) = 'Low';
    elseif strcmp(re1,'Not applicable')
        domain.rea(i) = 'Not applicable';
    end
end
%% 
for i=1:height(CA_table_new) % f: Flow and Timing
    f1 = CA_table_new{i,{'f1'}}; f2 = CA_table_new{i,{'f2'}}; f3 = CA_table_new{i,{'f3'}}; 
    f4 = CA_table_new{i,{'f4'}}; f5 = CA_table_new{i,{'f5'}};
    if strcmp(f1,'No') || strcmp(f2,'No') || strcmp(f3,'No') || strcmp(f5,'No')
        domain.f(i) = 'High';
    elseif strcmp(f1,'Unclear') || strcmp(f2,'Unclear') || strcmp(f3,'Unclear')|| strcmp(f5,'Unclear')
        domain.f(i) = 'Unclear';
    elseif strcmp(f2,'Not applicable') || strcmp(f5,'Not applicable') 
        domain.f(i) = 'ERROR'; % Possible missed entry
    elseif strcmp(f1,'Yes') && strcmp(f2,'Yes') && strcmp(f3,'Yes') && strcmp(f5,'Yes') 
        domain.f(i) = 'Low';
    end
end

CA_table_combined = [CA_table_new(:,[1,2,4]), domain]; % This table contains the classification of all studies
%% 6) For the consensus-meeting studies, hardcode the classification (to make sure that classification is correct)
% 107-1/2/3:    Ref extra --> Unclear
% 663-1/2:      Ref extra --> Unclear
% 683-1,2,3:    Ref extra --> Unclear
% 1318-2:       Ref extra --> Unclear
% 1321-1:       Ref extra --> Unclear
% 1369 1/2/3/4: Ref extra --> High
% 37 -1/2/3/4   Ref extra --> Unclear
consensus_ref_extra = ...
    table([107,107,107,663,663,683,683,683,1318,1321,1369,1369,1369,1369,37,37,37,37]',...
    [1,2,3,1,2,1,2,3,2,1,1,2,3,4,1,2,3,4]',...
    ["Unclear","Unclear","Unclear","Unclear","Unclear","Unclear","Unclear","Unclear","Unclear","Unclear","High","High","High","High","Unclear","Unclear","Unclear","Unclear"]',...
    ["re","re","re","re","re","re","re","re","re","re","re","re","re","re","re","re","re","re"]',...
    'VariableNames',{'articleID','studyID','Classification','Domain'});
% 130-17-30:    Ref intra--> Low
% 1321-1:       Ref intra --> Unclear
% 1369 1/2/3/4: Ref intra --> High
consensus_ref_intra = ...
    table([130,130,130,130,130,130,130,130,130,130,130,130,130,130,1321,1369,1369,1369,1369]',...
    [17,18,19,20,21,22,23,24,25,26,27,28,29,30,1,1,2,3,4]',...
    ["Low","Low","Low","Low","Low","Low","Low","Low","Low","Low","Low","Low","Low","Low","Unclear","High","High","High","High"]',...
    ["ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri","ri"]',...
    'VariableNames',{'articleID','studyID','Classification','Domain'});
% 48-1:         Perf mod --> High
% 224-1:        Perf mod --> Unclear
% 452-1:        Perf mod --> Low
% 510-2:        Perf mod --> Unclear
% 730-1/2/3/4:  Perf mod --> Low
% 41-7/8/9      Perf mod --> Unclear
consensus_perf_mod = ...
    table([48,224,452,510,730,730,730,730,41,41,41]',...
    [1,1,1,2,1,2,3,4,7,8,9]',...
    ["High","Unclear","Low","Unclear","Low","Low","Low","Low","Unclear","Unclear","Unclear"]',...
    ["p","p","p","p","p","p","p","p","p","p","p"]',...
    'VariableNames',{'articleID','studyID','Classification','Domain'});
% 1318-2:       Ref extra - Applicability --> Unclear
% 1321-1:       Ref extra - Applicability --> Unclear
consensus_ref_extra_applic = ...
    table([1318,1321]',...
    [2,1]',...
    ["Unclear","Unclear"]',...
    ["rea","rea"]',...
    'VariableNames',{'articleID','studyID','Classification','Domain'});
% 1321-1:       Ref intra - Applicability --> Unclear
consensus_ref_intra_applic = ...
    table([1321]',...
    [1]',...
    ["Unclear"]',...
    ["ria"]',...
    'VariableNames',{'articleID','studyID','Classification','Domain'});

consensus_all = [consensus_ref_extra;consensus_ref_intra;consensus_perf_mod;consensus_ref_extra_applic;consensus_ref_intra_applic];

%%
% 6-sub) Correct classification based on the consensus meeting
CA_table_combined_consensus = CA_table_combined; 
for ii = 1:height(consensus_all)
    selected_row = find(  CA_table_combined_consensus.ArticleID == consensus_all.articleID(ii)...
                        & CA_table_combined_consensus.StudyID   == consensus_all.studyID(ii) );
%     CA_table_combined_consensus(selected_row,consensus_all.Domain(ii)) = consensus_all.Classification(ii);
    CA_table_combined_consensus.(consensus_all.Domain(ii))(selected_row) = consensus_all.Classification(ii);
end

%% 7) Separation on real substudies and then CA classification
% From DA_table (check versions), obtain studyID_real
% Find the row numbers of unique substudies (unique articleId & studyID_real)
% For each unique substudy, perform CA-classification
% ---> Per domain, determine 'highest' bias score --> Put in new table value 
%        --> Build CA_tabled_combined_consensus_substudies

articleId_unique    = unique(DA_table.articleId);
studyID_real_unique = unique(DA_table.studyID_real);
CA_table_combined_consensus_substudies = CA_table_combined_consensus;
CA_table_combined_consensus_substudies_BACKUP = CA_table_combined_consensus_substudies; % 2022-07-26 to check classification of substudies
for i=1:length(articleId_unique)
    for ii=1:max(studyID_real_unique)
%         DA_table([DA_table.articleId==articleId_unique(i) & DA_table.studyID_real==studyID_real_unique(ii)],:)
        CA_table_combined_consensus_substudies_temp = CA_table_combined_consensus([DA_table.articleId==articleId_unique(i) & DA_table.studyID_real==studyID_real_unique(ii)],:);
        
        % Perform new classification
        variable_names = CA_table_combined_consensus_substudies_temp.Properties.VariableNames(4:13);
        for iii = 1:length(variable_names)
            classifications_per_variable = CA_table_combined_consensus_substudies_temp(:,variable_names(iii));
            if sum(strcmp("High",classifications_per_variable.(1))) ~= 0
                CA_table_combined_consensus_substudies_temp(:,variable_names(iii)) = {"High"};
            elseif sum(strcmp("Unclear",classifications_per_variable.(1))) ~= 0
                CA_table_combined_consensus_substudies_temp(:,variable_names(iii)) = {"Unclear"};
            elseif sum(strcmp("Low",classifications_per_variable.(1))) ~= 0
                CA_table_combined_consensus_substudies_temp(:,variable_names(iii)) = {"Low"};
            elseif sum(strcmp("Not applicable",classifications_per_variable.(1))) ~= 0
                CA_table_combined_consensus_substudies_temp(:,variable_names(iii)) = {"Not applicable"};
            else
                CA_table_combined_consensus_substudies_temp(:,variable_names(iii)) = {"ERROR"};
            end
        end
        CA_table_combined_consensus_substudies([DA_table.articleId==articleId_unique(i) & DA_table.studyID_real==studyID_real_unique(ii)],:) = ...
            CA_table_combined_consensus_substudies_temp;              
    end
end 

% Selected only the rows that belong to a real substudy, identified by 'DA_table.studyID_real'
% You go from a table of size 150x13 to 116x13
[~,selected_rows_substudies,~] = unique([DA_table.articleId,DA_table.studyID_real],'rows');
CA_table_combined_consensus_substudies_selected = CA_table_combined_consensus_substudies(selected_rows_substudies,:);
DA_table_selected116 = DA_table(selected_rows_substudies,:);

writetable(DA_table_selected116(:,2:15),strcat(data_folder,'output_matlab\','DA_table_selected116.csv'),'Delimiter','tab');
%% 8) Removal of studies with <50% Low RoB/Applicability classifications
percentages_Low_for_CAcutoff = [];
for i=1:height(CA_table_combined_consensus_substudies_selected)
    CA_per_study = CA_table_combined_consensus_substudies_selected{i,["s","i","p","ri","re","f","sa","ia","ria","rea"]};
    sum_not_NA          = sum(~strcmp(CA_per_study,"Not applicable"));
    percentage_Low_for_CAcutoff      = sum(strcmp(CA_per_study,"Low"))/sum_not_NA;
    
    percentages_Low_for_CAcutoff = [percentages_Low_for_CAcutoff; percentage_Low_for_CAcutoff];
end
CA_table_combined_consensus_substudies_selected_50cutoff = CA_table_combined_consensus_substudies_selected(percentages_Low_for_CAcutoff>=0.5,:);

%% 9) Calculate statistics for the QUADAS excel bar chart sheet

CA_table_combined_consensus_substudies_selected_Hb     = CA_table_combined_consensus_substudies_selected(...
    [strcmp(CA_table_combined_consensus_substudies_selected.NIRS_parameter,'Resting state NIRS (OxyHb+HHb)')|...
     strcmp(CA_table_combined_consensus_substudies_selected.NIRS_parameter,'fNIRS (OxyHb+HHb)')]              ,:);
CA_table_combined_consensus_substudies_selected_OxyInd = CA_table_combined_consensus_substudies_selected(...
     strcmp(CA_table_combined_consensus_substudies_selected.NIRS_parameter,'Oxygenation index (TSI/TOI/SrO2)'),:);

% 7-sub) Count the number of classifications per NIRS-parameter (i: OxyHb, fNIRS, OxyInd), ...
% domain (ii: 's','i',etc...), and classification (iii: 'Unclear', 'Low', etc..)
variable_names_statistics = {'f','p','re','ri','i','s','rea','ria','ia','sa'};
CA_statistics = [table(["Low","High","Unclear","Not applicable","To do"]','VariableNames',{'Classification'}), array2table(NaN(5,length(variable_names_statistics)),'VariableNames',variable_names_statistics)];
for i = [1,2]% Per NIRS-parameter
    if     i==1; CA_table_combined_consensus_substudies_selected_stat = CA_table_combined_consensus_substudies_selected_Hb;
% OLD    elseif i==2; CA_table_combined_consensus_substudies_selected_stat = CA_table_combined_consensus_substudies_selected_fNIRS;
    elseif i==2; CA_table_combined_consensus_substudies_selected_stat = CA_table_combined_consensus_substudies_selected_OxyInd;
    end
    for ii=1:length(variable_names_statistics) % Per Domain
        for iii=1:height(CA_statistics) % Per Classification
            CA_statistics.(variable_names_statistics{ii})(iii) = sum(strcmp(CA_table_combined_consensus_substudies_selected_stat.(variable_names_statistics{ii}),CA_statistics.Classification(iii)));
        end
    end
    if     i==1; CA_statistics_Hb    = CA_statistics;
%     elseif OLD i==2; CA_statistics_fNIRS = CA_statistics;
    elseif i==2; CA_statistics_OxyInd= CA_statistics;
    end
end

% CA_statistics_Hb and CA_statistics_OxyInd are used in the QUADAS-2 Excel
% Bar Chart sheet
writetable(CA_table_combined, strcat(data_folder,'output_matlab\',output_file_name),'Delimiter','tab');
%% 10) Calculate the 'Sum quality score' for each study - For Circle size correlation figure (Figure 3&4)
percentages_Low = [];
for i=1:height(CA_table_combined_consensus_substudies_selected_50cutoff)
    CA_per_study = CA_table_combined_consensus_substudies_selected_50cutoff{i,["s","i","p","ri","re","f","sa","ia","ria","rea"]};
    sum_not_NA          = sum(~strcmp(CA_per_study,"Not applicable"));
    percentage_Low      = sum(strcmp(CA_per_study,"Low"))/sum_not_NA;
    percentages_Low = [percentages_Low; percentage_Low];
end